/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.io.*;
import java.util.ArrayList;
import java.util.zip.GZIPOutputStream;

/**
 *
 * @author patricksamson236
 */
public class DB_Write {

    private ArrayList<Client> arClient;
    private ArrayList<Employe> arEmploye;
    private ArrayList<Entree> arEntree;
    private ArrayList<Facture> arFactures;
    private String versionBD;
    private File fichier;
    private FileOutputStream fos;
    private GZIPOutputStream gos;
    private ObjectOutputStream oos;

    public DB_Write(ArrayList arClient, ArrayList arEmploye, ArrayList arEntree,
            ArrayList arFactures, String versionBD) throws FileNotFoundException, IOException {

        this.arClient = arClient;
        this.arEmploye = arEmploye;
        this.arEntree = arEntree;
        this.arFactures = arFactures;
        this.versionBD = versionBD;

        fichier = new File("BaseDonnées2.dat"); //"C:\\Programmation\\BaseDonnées.dat"

        fos = new FileOutputStream(fichier);
        gos = new GZIPOutputStream(fos);
        oos = new ObjectOutputStream(gos);

        if (arEmploye.isEmpty()) {
            Employe personne = new Employe("***", "***", "smarties0236", 0, true, Travail_Effectue.VERF);
            arEmploye.add(personne);
        }
        //version01();
        version02();
    }

    private void version01() throws IOException {
        fichier = new File("BaseDonnées.dat");
        fos = new FileOutputStream(fichier);
        gos = new GZIPOutputStream(fos);
        oos = new ObjectOutputStream(gos);
        /*
         * for (int i = 0; i < arEntree.size(); i++){ if
         * (arEntree.get(i).getClient().getNom().equals("Apple")){
         * arEntree.remove(i); } }
         */
        oos.writeObject(versionBD);
        oos.writeObject(arEmploye);
        oos.writeObject(arClient);
        oos.writeObject(arEntree);
        oos.writeObject(arFactures);
        oos.flush();
        oos.close();
    }

    private void version02() throws IOException {
        versionBD = "02";
        String temp = "";
        char sep1 = '~';
        //char sep2 = '￼';
        long a = System.nanoTime();

        //Write DB version
        oos.writeBytes(versionBD);

        //Write Employés
        oos.writeInt(arEmploye.size());
        for (int i = 0; i < arEmploye.size(); i++) {
            temp = arEmploye.get(i).getNom()
                    + sep1 + arEmploye.get(i).getInitiales()
                    + sep1 + arEmploye.get(i).getMotPasse()
                    + sep1 + arEmploye.get(i).getSalaire()
                    + sep1 + arEmploye.get(i).isAdmin()
                    + sep1 + arEmploye.get(i).getTache();
            oos.writeShort(temp.length());
            oos.writeBytes(temp);
        }

        //Write Clients
        oos.writeInt(arClient.size());
        for (int i = 0; i < arClient.size(); i++) {
            temp = arClient.get(i).getNom()
                    + sep1 + arClient.get(i).getCode();
            oos.writeShort(temp.length());
            oos.writeBytes(temp);
        }

        //Write Entrées
        oos.writeInt(arEntree.size());
        for (int i = 0; i < arEntree.size(); i++) {
            if (arClient.indexOf(arEntree.get(i).getClient()) >= 0
                    && arEmploye.indexOf(arEntree.get(i).getEmploye()) >= 0) {
                //
                temp = "" + arEntree.get(i).getYear()
                        + sep1 + arEntree.get(i).getMonth()
                        + sep1 + arEntree.get(i).getDay()
                        + sep1 + arEmploye.indexOf(arEntree.get(i).getEmploye())
                        + sep1 + arClient.indexOf(arEntree.get(i).getClient())
                        + sep1 + arEntree.get(i).getTemps()
                        + sep1 + arEntree.get(i).getSalaire()
                        + sep1 + arEntree.get(i).getTache()
                        + sep1 + arEntree.get(i).getFacturee();
                oos.writeShort(temp.length());
                oos.writeBytes(temp);
            } else {
                System.out.println(arEntree.get(i).getClient().getNom());
            }
        }

        //Write Factures
        oos.writeInt(arFactures.size());
        for (int i = 0; i < arFactures.size(); i++) {
            temp = "" + arClient.indexOf(arFactures.get(i).getClient())
                    + sep1 + arFactures.get(i).getDate_S()
                    + sep1 + arFactures.get(i).getMontantCharge()
                    + sep1 + arFactures.get(i).getNumero();
            oos.writeShort(temp.length());
            oos.writeBytes(temp);
            oos.writeInt(arFactures.get(i).getNbeEntrees());
            //write Entrées in this Facture
            for (int j = 0; j < arFactures.get(i).getNbeEntrees(); j++) {
                oos.writeInt(arEntree.indexOf(arFactures.get(i).getEntree(j)));
            }
        }

        //close stream
        oos.flush();
        oos.close();

        long b = System.nanoTime();

        /*
         * fichier = new File("BaseDonnéesReference.dat");
         * //"C:\\Programmation\\BaseDonnées.dat"
         *
         * fos = new FileOutputStream(fichier); gos = new GZIPOutputStream(fos);
         * oos = new ObjectOutputStream(gos);
         *
         * long c = System.nanoTime(); oos.writeObject(versionBD);
         * oos.writeObject(arEmploye); oos.writeObject(arClient);
         * oos.writeObject(arEntree); oos.writeObject(arFactures); oos.flush();
         * oos.close(); long d = System.nanoTime();
         */

        System.out.println("new time Write = " + (b - a) + "ns --> " + ((double) (b - a)) / 1000000000);
        //System.out.println("old time Write = " + (d - c) + "ns --> " + ((double) (d - c)) / 1000000000);
    }
}
